package com.yanqu.road.entity.activity;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yanqu.road.utils.config.Config;
import com.yanqu.road.utils.string.StringUtils;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 活动配置
 */
public class ActivityConfig {
    private int activityId;     //活动id
    private int id;             //id
    private String param;       //参数名
    private String value;       //参数值

    public int getActivityId() {
        return activityId;
    }

    public void setActivityId(int activityId) {
        this.activityId = activityId;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getParam() {
        return param;
    }

    public void setParam(String param) {
        this.param = param;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public int getIntValue() {
        return Integer.parseInt(value.trim());
    }

    public long getLongValue() {
        return Long.parseLong(value.trim());
    }

    public List<Integer> getIntListValue(String separator) {
        return StringUtils.stringToIntegerList(value, separator);
    }

    public List<Long> getLongListValue(String separator) {
        return StringUtils.stringToLongList(value, separator);
    }

    public List<String> getStringListValue(String separator) {
        return StringUtils.stringToStringList(value, separator);
    }

    public static <T> T parseActivityConfig(Map<String, ActivityConfig> activityConfigMap, Class<T> clz) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("{");
        for (ActivityConfig activityConfig : activityConfigMap.values()) {
            stringBuilder.append(String.format("\"%s\":\"%s\",", activityConfig.getParam(), activityConfig.getValue()));
        }
        stringBuilder.deleteCharAt(stringBuilder.length() - 1);
        stringBuilder.append("}");
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        try {
            T t = objectMapper.readValue(stringBuilder.toString(), clz);
            return t;

        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return null;
    }
}
